<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * Class OrderProduct
 * @package App\Models
 * @property Collection $consumable
 * @property ProductInventory $inventory
 * @property Product $product
 */
class OrderProduct extends Model
{
    protected $fillable = ['product_id', 'attribute','expire', 'quantity', 'price','expiry'];

    protected static function boot()
    {
        static::addGlobalScope('orderBy', function (Builder $query) {
            $query->orderByDesc('id');
        });
        parent::boot();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function product()
    {
        return $this->belongsTo(Product::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function order()
    {
        return $this->belongsTo(Order::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * @author luffyzhao@vip.126.com
     */
    public function inventory(){
        return $this->hasOne(ProductInventory::class, 'product_id', 'product_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     * @author luffyzhao@vip.126.com
     */
    public function consumable()
    {
        return $this->belongsToMany(Consumable::class, 'product_consumable', 'product_id', 'consumable_id', 'product_id', 'id');
    }
}
